package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/upload")
@Slf4j
public class FileUploadController {
    
    @Autowired
    private OssService ossService;
    
    /**
     * 上传单个文件
     */
    @PostMapping("/single")
    public Result<Map<String, String>> uploadSingle(@RequestParam("file") MultipartFile file,
                                                   @RequestParam(value = "folder", defaultValue = "images/") String folder) {
        try {
            if (file.isEmpty()) {
                return Result.error("文件不能为空");
            }
            
            String fileUrl = ossService.uploadFile(file, folder);
            
            Map<String, String> result = new HashMap<>();
            result.put("fileName", file.getOriginalFilename());
            result.put("fileUrl", fileUrl);
            result.put("fileSize", String.valueOf(file.getSize()));
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("文件上传失败", e);
            return Result.error("文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 上传文件并指定文件名（用于鱼类图片）
     */
    @PostMapping("/fish-image")
    public Result<Map<String, String>> uploadFishImage(@RequestParam("file") MultipartFile file,
                                                       @RequestParam("fishName") String fishName) {
        try {
            if (file.isEmpty()) {
                return Result.error("文件不能为空");
            }
            
            // 清理文件名中的特殊字符
            String cleanFishName = fishName.replaceAll("[^\\u4e00-\\u9fa5a-zA-Z0-9]", "_");
            String fileUrl = ossService.uploadFileWithName(file, "fish-images/", cleanFishName);
            
            Map<String, String> result = new HashMap<>();
            result.put("fishName", fishName);
            result.put("fileName", file.getOriginalFilename());
            result.put("fileUrl", fileUrl);
            result.put("fileSize", String.valueOf(file.getSize()));
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("鱼类图片上传失败", e);
            return Result.error("鱼类图片上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量上传文件
     */
    @PostMapping("/batch")
    public Result<Map<String, Object>> uploadBatch(@RequestParam("files") MultipartFile[] files,
                                                  @RequestParam(value = "folder", defaultValue = "images/") String folder) {
        try {
            Map<String, Object> result = new HashMap<>();
            Map<String, String> successFiles = new HashMap<>();
            Map<String, String> failedFiles = new HashMap<>();
            
            for (MultipartFile file : files) {
                try {
                    if (!file.isEmpty()) {
                        String fileUrl = ossService.uploadFile(file, folder);
                        successFiles.put(file.getOriginalFilename(), fileUrl);
                    }
                } catch (Exception e) {
                    failedFiles.put(file.getOriginalFilename(), e.getMessage());
                }
            }
            
            result.put("successFiles", successFiles);
            result.put("failedFiles", failedFiles);
            result.put("totalCount", files.length);
            result.put("successCount", successFiles.size());
            result.put("failedCount", failedFiles.size());
            
            return Result.success(result);
            
        } catch (Exception e) {
            log.error("批量文件上传失败", e);
            return Result.error("批量文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除文件
     */
    @DeleteMapping("/delete")
    public Result<String> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        try {
            ossService.deleteFile(fileUrl);
            return Result.success("文件删除成功");
        } catch (Exception e) {
            log.error("文件删除失败", e);
            return Result.error("文件删除失败: " + e.getMessage());
        }
    }
}